﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using OF.Common;
using System.Data;
namespace OF.Web.admin
{
    public partial class userinfo : ShopAdminPage
    {

        BLL.User.Seller bll_seller = new BLL.User.Seller();
        Model.Seller model;
        int id = 0;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                id = WebUtils.GetId();
                //绑定下拉
                string path = Server.MapPath("res/js/shoptype.xml");
                ddlShoptype.DataSource = OF.Common.Utils.GetXmlToDropdownlist(path);
                ddlShoptype.DataTextField = "key";
                ddlShoptype.DataValueField = "value";
                ddlShoptype.DataBind();

                #region 绑定信息
                model = bll_seller.GetModel(id);
                if (model != null)
                {
                    hfid.Value = id.ToString();
                    txtshopname.Text = model.shopname;
                    txtaddress.Text = model.shopaddress;
                    txtspend.Text = model.average_spend == null ? "" : model.average_spend.ToString();
                    txtphone.Text = model.phone;
                   
                    txtfloorprice.Text = model.floorprice == null ? "" : model.floorprice.ToString();
                    txttip.Text = model.tip == null ? "" : model.tip.ToString();
                    txtarrivtime.Text = model.arrivtime == null ? "" : model.arrivtime.ToString();
                    ddlShoptype.SelectedValue = model.shoptype;
                    if (!string.IsNullOrWhiteSpace(model.shopphoto))
                        imglitpic.ImageUrl = model.shopphoto;
                    txtfeature.Text = model.feature;
                    //绑定学校列表
                    BindSchool(model.id);
                    //绑定送餐时间
                    BindTime(model.delivery_time);
                    rblIsonline.SelectedValue = model.isopen?"1":"0";
                    //绑定状态
                    rblState.SelectedValue=model.state.ToString();
                    
                }
                else
                {
                    BindSchool(0);
                }
                #endregion
            }
        }

        #region 绑定学校

        void BindSchool(int id)
        {
            //获取所有学校 
            DataSet ds = bll_seller.GetSchoolList();
            rptList.DataSource = ds;
            rptList.DataBind();

            if (id > 0 && rptList != null && rptList.Items.Count > 0)
            {
                //获取商家附近学校
                var temp = bll_seller.GetSchoolListBySeller(id);
                if (temp == null || temp.Tables[0] == null)
                    return;
                int count = temp.Tables[0].Rows.Count;
                for (int i = 0; i < count; i++)
                {
                    hfSchools.Value += temp.Tables[0].Rows[i]["schoolid"] + ",";
                }
                //初始化选中状态
                count = rptList.Items.Count;
                for (var i = 0; i < count; i++)
                {
                    CheckBox chbSelect = (CheckBox)rptList.Items[i].FindControl("chbSel");
                    if (hfSchools.Value.Contains(chbSelect.ValidationGroup))
                    {
                        chbSelect.Checked = true;
                    }
                }
            }
        }
        #endregion

        #region 绑定送餐时间
        void BindTime(string timestr) {
            if (string.IsNullOrWhiteSpace(timestr))
                return;
            //早中晚时间段分割
            string[] times = timestr.Split(';');
            if (times.Length < 3)
                return;
            string[] astr=times[0].Split('-');
            if (astr.Length == 2)
            {
                ddl1.SelectedValue = astr[0];
                ddl2.SelectedValue = astr[1];
            }

            string[] bstr = times[1].Split('-');
            if (bstr.Length == 2)
            {
                ddl3.SelectedValue = bstr[0];
                ddl4.SelectedValue = bstr[1];
            }
            string[] cstr = times[2].Split('-');
            if (cstr.Length == 2)
            {
                ddl5.SelectedValue = cstr[0];
                ddl6.SelectedValue = cstr[1];
            }
        }
        #endregion

    

        #region 上传图片确定事件
        protected void btnPicSure_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrWhiteSpace(fileUpLogo.FileName))
            {
                return;
            }
            UploadCommon upload = new UploadCommon();
            string result = upload.MakeThumbnailByMode(fileUpLogo, 0);
            if (!(result.IndexOf("ERROR") > -1 && result.Trim().Length == 7))//上传图片成功
            {
                hfPicName.Value = result;
                this.imglitpic.ImageUrl = result;
                this.lblShow.Text = "";
            }
            else
            {
                //上传图片失败
                this.lblShow.Text = "图片上传失败！";
            }
        }
        #endregion

        #region 加载学校
        private void LoadSchool()
        {
            string ids = "";
            for (var i = 0; i < rptList.Items.Count; i++)
            {
                CheckBox chbSelect = (CheckBox)rptList.Items[i].FindControl("chbSel");
                if (chbSelect.Checked)
                {
                    ids += chbSelect.ValidationGroup + ",";
                }
            }
            //添加信息
            hfSchools.Value = ids;
        }
        #endregion

        #region 提交

        protected void btnSave_Click(object sender, EventArgs e)
        {

            model = bll_seller.GetModel(Convert.ToInt32(hfid.Value));

            model.shopname = txtshopname.Text;
            model.shoptype = ddlShoptype.SelectedValue;
            model.shopaddress = txtaddress.Text;

            model.phone = txtphone.Text;
            model.average_spend = string.IsNullOrWhiteSpace(txtspend.Text) ? 0 : Convert.ToDecimal(txtspend.Text);
            model.order_type = Convert.ToInt32(rblType.SelectedValue);

            string timestr = "";
            bool result = checkTime(out timestr);
            if (!result)
            {
                return;
            }
            else
            {
                model.delivery_time = timestr;
            }
            model.floorprice = string.IsNullOrWhiteSpace(txtfloorprice.Text) ? 0 : Convert.ToDecimal(txtfloorprice.Text);
            model.tip = string.IsNullOrWhiteSpace(txttip.Text) ? 0 : Convert.ToDecimal(txttip.Text);
            model.arrivtime = string.IsNullOrWhiteSpace(txtarrivtime.Text) ? 0 : Convert.ToInt32(txtarrivtime.Text);


            model.shopphoto = imglitpic.ImageUrl;
            model.feature = txtfeature.Text;

            model.state = Convert.ToInt32(rblState.SelectedValue);
            model.isopen = rblIsonline.SelectedValue=="1"?true:false;
            //更新商家信息
            result = bll_seller.Update(model);
            if (result)
            {

                //删除附近学校
                int sellerid = Convert.ToInt32(hfid.Value);
                bll_seller.DeleteSellerSchool(sellerid);

                //插入详情
                LoadSchool();
                var array = hfSchools.Value.Split(',');
                for (int i = 0; i < array.Length - 1; i++)
                {
                    if (!bll_seller.AddSellerSchool(sellerid, array[i]))
                    {
                        MessageBox.Show(this, "操作失败");
                        return;
                    }
                }

                //更新缓存
                OF.BLL.Addr.AreaCache cache = new OF.BLL.Addr.AreaCache();
                cache.UpdateCache(OF.Common.Enmus.CACHE_TYPE.C_SELLER);
                cache.UpdateCache(OF.Common.Enmus.CACHE_TYPE.C_SELLERBELONG);

                MessageBox.Show(this, "操作成功");
            }
            else
            {
                MessageBox.Show(this, "操作失败");
            }
        }
        #endregion

        #region 验证并拼接早晚送餐时间
        private bool checkTime(out string str)
        {
            str = "";
            string d1 = ddl1.SelectedValue;
            string d2 = ddl2.SelectedValue;
            if ((d1 == "-1" && d2 != "-1") || (d1 != "-1" && d2 == "-1"))
            {
                MessageBox.Show(this, "请选择早上送餐开始和结束时间");
                return false;
            }
            string d3 = ddl3.SelectedValue;
            string d4 = ddl4.SelectedValue;
            if ((d3 == "-1" && d4 != "-1") || (d3 != "-1" && d4 == "-1"))
            {
                MessageBox.Show(this, "请选择中午送餐开始和结束时间");
                return false;
            }
            string d5 = ddl5.SelectedValue;
            string d6 = ddl6.SelectedValue;
            if ((d5 == "-1" && d6 != "-1") || (d5 != "-1" && d6 == "-1"))
            {
                MessageBox.Show(this, "请选择晚上送餐开始和结束时间");
                return false;
            }


            if (Convert.ToInt32(d1) <= Convert.ToInt32(d2))
            {
                str = d1 + "-" + d2 + ";";
            }
            else
            {
                MessageBox.Show(this, "早上送餐结束时间不能大于开始时间");
                return false;
            }
            //有效时间点拼接
            if (Convert.ToInt32(d3) <= Convert.ToInt32(d4))
            {
                str += d3 + "-" + d4 + ";";
            }
            else
            {
                MessageBox.Show(this, "中午送餐结束时间不能大于开始时间");
                return false;
            }

            //有效时间点拼接
            if (Convert.ToInt32(d5) <= Convert.ToInt32(d6))
            {
                str += d5 + "-" + d6 + ";";
            }
            else
            {
                MessageBox.Show(this, "晚上送餐结束时间不能大于开始时间");
                return false;
            }
            return true;
        }
        #endregion



    }
}